package com.itheima.reggie.service.iml;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品，同时插入菜品对应的口味数据，需要操作两张表：dish、dish_flavor
     * 新增菜品，同时保存对应的口味数据
     *
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
//        ①. 保存菜品基本信息 ;
        this.save(dishDto);
//        ②. 获取保存的菜品ID ;
        Long id = dishDto.getId();
//        ③. 获取菜品口味列表，遍历列表，为菜品口味对象属性dishId赋值;
        List<DishFlavor> flavors = dishDto.getFlavors();
//        方法一   为菜品口味对象属性dishId赋值;
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(id);
        }
        /*
        方法二     为菜品口味对象属性dishId赋值;
         flavors = flavors.stream().map((item) -> {
        item.setDishId(dishId);
        return item;
        }).collect(Collectors.toList());
         */
//        ④. 批量保存菜品口味列表;
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 菜品管理分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<DishDto> dishPage(int page, int pageSize, String name) {
        //构造分页构造器对象
        Page<Dish> dishPage = new Page<>(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        //添加过滤条件
        lqw.like(name != null, Dish::getName, name);
        //添加排序条件
        lqw.orderByDesc(Dish::getUpdateTime);
        //执行分页查询
        this.page(dishPage, lqw);

        //构造分页构造器对象
        Page<DishDto> dishDtoPage = new Page<>();
        //对象拷贝
        BeanUtils.copyProperties(dishPage, dishDtoPage, "records");
        //新建集合 用来存储dishDao内容
        List<DishDto> list = new ArrayList<>();
        //获得dishPage中的records集合
        List<Dish> records = dishPage.getRecords();
        //遍历集合
        for (Dish record : records) {
            //新建dishDto 存储菜品分类信息
            DishDto dishDto = new DishDto();
            //把菜品信息复制给dishDto
            BeanUtils.copyProperties(record, dishDto);
            //获得菜品分类的id
            Long categoryId = record.getCategoryId();
            //通过id查询菜品分类
            Category category = categoryService.getById(categoryId);
            //获得分类名称
            String categoryName = category.getName();
            //设置给dishDao
            dishDto.setCategoryName(categoryName);
            //添加到集合
            list.add(dishDto);
        }
        dishDtoPage.setRecords(list);
        return dishDtoPage;
    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     *
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息，从dish表查询
        Dish dish = this.getById(id);
        //新建dishDto对象 用于存储基本信息和口味信息
        DishDto dishDto = new DishDto();
        //复制基本信息
        BeanUtils.copyProperties(dish, dishDto);
        //构建条件 从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        //查根据id查询对应的口味信息 返回list集合
        lqw.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> list = dishFlavorService.list(lqw);
        //存储在dishDto中
        dishDto.setFlavors(list);
        return dishDto;
    }

    /**
     * 保存修改菜品
     *
     * @param dishDto
     */
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);
        //先根据id把原始的口味信息全部删除
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(lqw);
        //获取新修改后的口味信息集合,遍历集合,给每一个口味信息关联菜品id
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            Long id = dishDto.getId();
            flavor.setDishId(id);
        }
        //保存信息
        dishFlavorService.saveBatch(flavors);
    }
}
